package com.iraeta.controller.hik;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.hik.HikView;
import com.iraeta.service.hik.IHikViewService;
import com.iraeta.service.hik.IHikViewcameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController(value = "HikCameraViewController")
@RequestMapping("hikcameraview")
public class HikCameraViewController extends BaseController {
    @Autowired
    IHikViewService hikViewService;
    @Autowired
    IHikViewcameraService hikViewcameraService;

    @RequestMapping("pagecameraview")
    public String pageCameraView(@RequestBody JSONObject jo){
        String roleName = jo.getString("roleName");
        this.pageSet(jo);
        List<HikView> hikViewList = hikViewService.listHikView(this.getUserId(), roleName);
        PageInfo<HikView> hikViewPageInfo = this.getPageInfo(hikViewList);
        return JSON.toJSONString(Result.success(hikViewPageInfo));
    }

    @RequestMapping("getcameraview")
    public String getCameraView(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        HikView hikView = hikViewService.getById(id);
        return JSON.toJSONString(Result.success(hikView));
    }

    @RequestMapping("addeditcameraview")
    public String addCameraView(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        HikView hikView = id == 0 ? new HikView() :  hikViewService.getById(id);
        this.mapJsonToObj(paramJb, hikView);
        if(id == 0){
            hikViewService.save(hikView);
        }else {
            hikViewService.updateById(hikView);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deletecameraview")
    public String deleteCameraView(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        hikViewService.removeById(id);
        hikViewcameraService.deleteHikViewcameraByViewId(id);
        return JSON.toJSONString(Result.success());
    }
}
